package crm.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

import crm.control.SaleareaControl;
import crm.dbcoon.DBConnect;

public class AutoFind {
	 /*
     * 实现自动提示功能，返回Json（jsonString）
     */
    @SuppressWarnings("unchecked")
    public static String FindCustomer(String term){
    DBConnect dbc = new DBConnect();
    String jsonStr = "";
    String sql="";
    try {
  	 sql = "select id,name,area_id,business from info_customer where namepy like '%"+term+"%' or name like '%"+term+"%' ";
  	 dbc.prepareStatement(sql);
  	 System.out.print(sql);
  	 ResultSet rs = dbc.executeQuery();
  	 List rsList = new ArrayList();
  	 Map map = null;
  	 while(rs.next()){
  		map = new HashMap();
  		map.put("id",rs.getInt("id"));
  		map.put("name",rs.getString("name"));
  		String areaName = SaleareaControl.getAreaName(rs.getInt("area_id"));
  		map.put("areaName",areaName);
//  		System.out.println("WWWWW"+areaName+"WWWWWW");
  		map.put("business", rs.getString("business"));
  		rsList.add(map);
  	}
  	rs.close();
  	rs = null;
  	Gson gson = new Gson();
  	jsonStr = gson.toJson(rsList);
  	System.out.println("jsonStr="+jsonStr);
  } catch (SQLException e) {
  	// TODO Auto-generated catch block
  	e.printStackTrace();
  }finally{
  	dbc.close();
  	
  }
  return jsonStr;
    }
    
    @SuppressWarnings("unchecked")
	public static String FindEmployee(String term){
        DBConnect dbc = new DBConnect();
        String jsonStr = "";
        String sql="";
        try {
      	 sql = "select id,name,area_id,position from info_employees where namepy like '%"+term+"%' or name like '%"+term+"%' ";
      	 dbc.prepareStatement(sql);
      	 System.out.print(sql);
      	 ResultSet rs = dbc.executeQuery();
      	 List rsList = new ArrayList();
      	 Map map = null;
      	 while(rs.next()){
      		map = new HashMap();
      		map.put("id",rs.getInt("id"));
      		map.put("name",rs.getString("name"));
      		String areaName = SaleareaControl.getAreaName(rs.getInt("area_id"));
      		map.put("areaName",areaName);
      		//map.put("area_id",rs.getInt("area_id"));
      		String position = GetBaseParam.getPositionById(rs.getInt("position"));
      		map.put("position", position);
      		rsList.add(map);
      	}
      	rs.close();
      	rs = null;
      	Gson gson = new Gson();
      	jsonStr = gson.toJson(rsList);
      	System.out.println("jsonStr="+jsonStr);
      } catch (SQLException e) {
      	// TODO Auto-generated catch block
      	e.printStackTrace();
      }finally{
      	dbc.close();
      	
      }
      return jsonStr;
        }


}
